package com.ayitubie.refuelweb.controller;

import com.ayitubie.refuelweb.config.jenkins.JenkinsConnect;
import com.offbytwo.jenkins.JenkinsServer;
import com.offbytwo.jenkins.client.JenkinsHttpClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.offbytwo.jenkins.model.Job;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/jenkins")
public class JenkinsCtl {
    private JenkinsServer jenkinsServer;

    private JenkinsHttpClient jenkinsHttpClient;

    JenkinsCtl() {
        jenkinsServer = JenkinsConnect.connection();
        jenkinsHttpClient = JenkinsConnect.getClient();
    }

    @GetMapping("getJobs")
    public Map getJenkinsJobs() throws Exception {
        Map<String, Job> jobs = jenkinsServer.getJobs();
        return jobs;
    }

    @GetMapping("execJob")
    public void execJob() {
        try {
            Map<String, String> param = new HashMap<>();
            param.put("branch", "*/master");
            jenkinsServer.getJob("page-map-pipeline").build(param);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
